package org.linlinjava.litemall.admin.web;


import jodd.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.digester.Rules;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGrouponRules;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallRouponRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Validated
@RequestMapping("/admin/groupon")
public class AdminRouponRulesController {

    @Resource
    LitemallRouponRulesService litemallRouponRulesService;



    @RequiresPermissions("admin:groupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理","团购管理"},button = "团购查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1",required = false) Integer page,
                       @RequestParam(defaultValue = "1",required = false) Integer limit,
                       Integer goodsId, String sort, String order){
        return ResponseUtil.okList(litemallRouponRulesService.getAll(goodsId,page,limit));
    }

    @RequiresPermissions("admin:groupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理","团购管理"},button = "新增")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallGrouponRules rules){
        Object validated = validated(rules);
        if(validated != null){
            return validated;
        }
        boolean b = litemallRouponRulesService.addRules(rules);
        if(b){
            return ResponseUtil.ok();
        }
        return ResponseUtil.isNull();
    }

    @RequiresPermissions("admin:groupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理","团购管理"},button = "修改团购")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallGrouponRules rules){
        boolean b = litemallRouponRulesService.update(rules);
        if(b){
            return ResponseUtil.ok();
        }
        return ResponseUtil.isNull();
    }

    public Object validated(LitemallGrouponRules rules){
        try {
            if (rules.getExpireTime() == null) {
                return ResponseUtil.badArgument();
            }
            if (rules.getGoodsId() == 0) {
                return ResponseUtil.badArgument();
            }
            if (rules.getDiscount() == null) {
                return ResponseUtil.badArgument();
            }

            if (rules.getDiscountMember() == null) {
                return ResponseUtil.badArgument();
            }
        }catch (Exception e){
            return ResponseUtil.badArgument();
        }
        return null;
    }


    @RequiresPermissions("admin:groupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","团购管理"},button = "删除团购")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallGrouponRules rules){
        boolean b = litemallRouponRulesService.delete(rules);
        if(b){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }



/*    @RequiresPermissions("admin:groupon:listRecord")
    @RequiresPermissionsDesc(menu = {"推广管理","团购管理"},button = "删除团购")
    @GetMapping("/listRecord")
    public Object listRecord(LitemallGrouponRules rules){
        boolean b = litemallRouponRulesService.delete(rules);
        if(b){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }*/


}
